using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.Entidades;

namespace Marlimer.Data.Comercial
{
    public class his_ProductoDrop
    {
        public DataTable getProductoDrop(int nanio, int nmes, int flg_anulados)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sqlcmd.CommandText = "spsConsultaDropProductos";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@NANIO", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NMES", SqlDbType.Int);
            sqlcmd.Parameters.Add("@FLG_ANULADOS", SqlDbType.Int);

            sqlcmd.Parameters["@NANIO"].Value = nanio;
            sqlcmd.Parameters["@NMES"].Value = nmes;
            sqlcmd.Parameters["@FLG_ANULADOS"].Value = flg_anulados;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public resulMsgError updProductoDrop(List<InfoDropSizeProducto> dropprod)
        {
            resulMsgError resul = new resulMsgError(false, "");
            //creamos el objeto de transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("savedrop");

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spiaDropProductos";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac;

            sqlcmd.Parameters.Add("@NANIO", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NMES", SqlDbType.Int);
            sqlcmd.Parameters.Add("@CCOD_PROD", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NCANT_DROP", SqlDbType.Decimal);
            sqlcmd.Parameters.Add("@NSOLES_DROP", SqlDbType.Decimal);

            try
            {
                for (int i = 0; i < dropprod.Count; i++)
                {
                    sqlcmd.Parameters["@NANIO"].Value = dropprod[i].nanio;
                    sqlcmd.Parameters["@NMES"].Value = dropprod[i].nmes;
                    sqlcmd.Parameters["@CCOD_PROD"].Value = dropprod[i].ccod_prod;
                    sqlcmd.Parameters["@NCANT_DROP"].Value = dropprod[i].ncant_drop;
                    sqlcmd.Parameters["@NSOLES_DROP"].Value = dropprod[i].nsoles_drop;
                    sqlcmd.ExecuteNonQuery();
                }
                transac.Commit();
                resul.resul = true;
            }
            catch (Exception ex)
            {
                transac.Rollback();
                resul.message = ex.Message;
            }
            return resul;
        }
    }
}
